package com.mit.web.business;

import java.util.List;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.exception.BaseException;
import com.mit.common.sale.dto.CustomerDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.SaleTokens;
import com.mit.common.token.Tokens;

public class SaleService extends RemoteService {
	private final String FACADE_NAME = "SaleFacade";
	@Override
	public String getFacadeName() {

		return FACADE_NAME;
	}

	public CustomerDTO saveCustomer(CustomerDTO to,UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveCustomer", currentUser);
		rqs.addParameter(SaleTokens.KEY_CUSTOMER_DTO, to);
		ResultContext rlt  = ServiceLocator.getInstance().doBusiness(rqs);	
		CustomerDTO rtn = (CustomerDTO) rlt.getResult(SaleTokens.KEY_CUSTOMER_DTO);
		return rtn;

	}
	
	public List<CustomerDTO> retrievePagedCustomers(String searchKey, Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedCustomers", currentUser);
		rqs.addParameter(SaleTokens.KEY_CUSTOMER_NAME, searchKey);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<CustomerDTO> rtn = (List<CustomerDTO>) rlt.getResult(SaleTokens.KEY_CUSTOMER_LIST);
		return rtn;
	}
	
	public Integer retrieveCustomerCount(String searchKey, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveCustomerCount", currentUser);
		rqs.addParameter(SaleTokens.KEY_CUSTOMER_NAME, searchKey);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		Integer rtn = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rtn;
	}
	
	public List<CustomerDTO> retrieveCustomerBasicInfo(String searchName,  UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveCustomerBasicInfo", currentUser);
		rqs.addParameter(SaleTokens.KEY_CUSTOMER_NAME, searchName);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<CustomerDTO> rtn = (List<CustomerDTO>) rlt.getResult(SaleTokens.KEY_CUSTOMER_LIST);
		return rtn;
	}
}
